/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.thinkinbunny.control;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Iterator;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.thinkinbunny.data.AndroidVersion;
import org.thinkinbunny.data.DbHelper;
import org.thinkinbunny.httpUtil.ProcessRequest;

/**
 *
 * @author yfxiawei
 */
public class AndroidUpdate extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
   response.setContentType("text/html;charset=UTF-8");
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);

   request.setCharacterEncoding("UTF-8");
        if (isMultipart) {

//下面这两行构造文件上传处理对象  
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);

//upload.setHeaderEncoding("UTF-8");
            Iterator items;
            try {
//下面这行解析表单提交的所有文件内容  
                items = upload.parseRequest(request).iterator();
                while (items.hasNext()) {
                    FileItem item = (FileItem) items.next();
                    if (item.isFormField()) {
//                                 if (item.getFieldName().equals("verinfo")) {
//                              System.out.println("verinfo"+item.getString());
//                        }
//                                           if (item.getFieldName().equals("vercontent")) {
//                              System.out.println("vercontent"+URLDecoder.decode(item.getString(), "UTF-8"));
//                        }
//                                                     if (item.getFieldName().equals("url")) {
//                              System.out.println("url"+item.getString());
//                        }
                    }
                    if (!item.isFormField()) {



//下面这段代码取出文件名,和服务器的存储路径,上传的文件将被存储在当前项目的file文件夹  
                        String name = item.getName();
                        String fileName = name.substring(
                                name.lastIndexOf("//") + 1, name.length());
                        String path = request.getRealPath("/update")
                                + File.separatorChar + fileName;
                        //
                        System.out.println(name);
                        String temp=name.substring(name.lastIndexOf("\\"));
                        // 上传文件  
                        // System.out.println("name"+name);
                       //  System.out.println("fileName"+fileName);
                       
               
                        path = request.getRealPath("/update/")+temp;
                         System.out.println("path"+path);
                        File uploadedFile = new File(path);
                        item.write(uploadedFile);


                        // 打印上传成功信息  
                        response.setContentType("text/html");
                        response.setCharacterEncoding("utf-8");
                        PrintWriter out = response.getWriter();
                        out.print("<center>");
                        out.print("<font size=10>上传的文件名为:" + name + "<br>");
                        out.print("保存的地址为:" + temp + "</font><br>");
                        out.println(" <a href='MangerServer.jsp'>返回管理</a>");
                          out.print("<center>");
                        
                    }

                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
      String update=  request.getParameter("update");
      if(update!=null&&update.equals("details")){
          AndroidVersion androidVersion=new AndroidVersion();
          androidVersion.setVersion(request.getParameter("version"));
          androidVersion.setUpdatecontent(request.getParameter("content"));
          androidVersion.setUpdateUrl(request.getParameter("url"));
          DbHelper dbHelper=new DbHelper();
          try {
              dbHelper.OpenDb();
          
          dbHelper.updateAndroidversion(androidVersion);
           PrintWriter out = response.getWriter();
            out.print("<center>");
                        out.print("<font size=15>成功修改版本信息<br>");
                      
                        out.println(" <a href='MangerServer.jsp'>返回管理</a>");
                        out.print("</center>");
          } catch (Exception e) {
            //  ProcessRequest.processRequestError(request, response);
          }finally{
          dbHelper.CLoseDb();;}
          
          
      
      }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
